﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MsgPack.Serialization;
using System.IO;
using SuperSocket.ProtoBase;
using IPrinting;

namespace PhotoPrintClient
{
    public class PrintResponsePackageInfo : IPackageInfo
    {
        public string Key
        {
            get 
            {
                return Result.ToString();
            }
        }

        public PrintResponsePackageInfo(PrintResult result, byte[] bodyBuffer)
        {
            Result = result;
            BodyData = bodyBuffer;
        }

        /// <summary>
        /// 服务器返回的字节数据头部
        /// </summary>
        private PrintResult Result = PrintResult.InitOK;

        /// <summary>
        /// 服务器返回的字节数据
        /// </summary>
        public byte[] BodyData { get; set; }

        /// <summary>
        /// 服务器返回的字符串数据
        /// </summary>
        public PrintResponsePackge Packge
        {
            get
            {
                if (BodyData == null || BodyData.Length == 0)
                {
                    return null;
                }
                var serializer = SerializationContext.Default.GetSerializer<PrintResponsePackge>();
                using (var stream = new MemoryStream(BodyData, 0, BodyData.Length))
                {
                    PrintResponsePackge pack = serializer.Unpack(stream) as PrintResponsePackge;
                    return pack;
                }
            }
        }
    }
}
